package com.strelitzia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.strelitzia.dao.AlbumDao;
import com.strelitzia.entity.AlbumEntity;
import com.strelitzia.entity.PhotoEntity;
import com.strelitzia.filter.CorsFilter;
import com.strelitzia.service.AlbumService;
import com.strelitzia.service.PhotoService;
import com.strelitzia.vo.AlbumItem;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @create 2024/1/12 13:52
 */
@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumDao, AlbumEntity> implements AlbumService {
    @Autowired
    PhotoService photoService;

    @Override
    public void create(String name) {
        AlbumEntity album = this.getOne(new QueryWrapper<AlbumEntity>().eq("album_name", name).eq("uid", CorsFilter.loginUserId.get()));
        if (album == null) {
            AlbumEntity albumEntity = new AlbumEntity();
            albumEntity.setAlbumName(name);
            albumEntity.setCreateTime(new Timestamp(new Date().getTime()));
            albumEntity.setUpdateTime(albumEntity.getCreateTime());
            albumEntity.setCover("");
            albumEntity.setUid(CorsFilter.loginUserId.get());
            if ("动态相册".equals(name)) {
                albumEntity.setStatus(1);
            } else {
                albumEntity.setStatus(0);
            }
            save(albumEntity);
        } else {
            int index = 1;
            String[] split = name.split("\\(");
            if (split.length > 0) {
                String str = split[split.length - 1].replace(")", "");
                try {
                    index = Integer.parseInt(str);
                    index = index + 1;
                    int i = name.lastIndexOf("(");
                    name = name.substring(0,i);
                } catch (NumberFormatException e) {
                    // 忽略异常
                }
            }
            create(name + "(" + index + ")");
        }

    }

    @Override
    public List<AlbumItem> getList(Integer type) {
        QueryWrapper<AlbumEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", CorsFilter.loginUserId.get());
        switch (type) {
            case 0:
                queryWrapper.orderByDesc("update_time").orderByDesc("create_time");
                break;
            case 1:
                queryWrapper.orderByDesc("create_time");
                break;
            case 2:
                queryWrapper.orderByAsc("create_time");
                break;
            case 3:
                queryWrapper.eq("status", 0).orderByDesc("update_time").orderByDesc("create_time");
        }
        List<AlbumEntity> list = this.list(queryWrapper);
        return getAlbumItems(list);
    }

    @Override
    public void del(Integer id) {
        PhotoEntity photoEntity = new PhotoEntity();
        photoEntity.setDelTag(1);
        photoService.update(photoEntity, new QueryWrapper<PhotoEntity>().eq("album_id", id));
        this.removeById(id);
    }

    @Override
    public List<AlbumItem> getFriendList(Integer id) {
        List<AlbumEntity> list = this.list(new QueryWrapper<AlbumEntity>()
                .eq("uid", id)
                .eq("status", 0)
                .orderByDesc("update_time")
                .orderByDesc("create_time"));
        return getAlbumItems(list);
    }

    private List<AlbumItem> getAlbumItems(List<AlbumEntity> list) {
        if (list != null) {
            return list.stream().map(item -> {
                AlbumItem albumItem = new AlbumItem();
                albumItem.setId(item.getId());
                albumItem.setStatus(item.getStatus());
                albumItem.setName(item.getAlbumName());
                albumItem.setCover(StringUtils.isNullOrEmpty(item.getCover()) ? "" : "/api/res/" + item.getCover());
                albumItem.setCount(photoService.count(new QueryWrapper<PhotoEntity>().eq("album_id", item.getId()).eq("del_tag", 0)));
                return albumItem;
            }).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }
}
